package com.hlz.flink.chapter05;

import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;

import java.security.spec.ECField;

/**
 * 随机分区
 * 随机分区服从均匀分布（uniform distribution），所以可以把流中的数据随机打乱，均匀地
 * 传递到下游任务分区，
 *
 * @author Hongliang Zhu
 * @create 2023-05-06 23:27
 */
public class ShufflePartitionTest {

    public static void main(String[] args) throws Exception {

        StreamExecutionEnvironment executionEnvironment = StreamExecutionEnvironment.getExecutionEnvironment();

        executionEnvironment.setParallelism(1);
        DataStreamSource<Event> source = executionEnvironment.fromElements(
                new Event("Mary", "./mary", 1000L),
                new Event("Bob", "./hello", 2000L),
                new Event("Alice", "./prod", 3000L),
                new Event("Alice", "./prod2", 4000L),
                new Event("Bob", "./prod", 5000L),
                new Event("Bob", "./prod1", 6000L),
                new Event("Bob", "./prod2", 7000L),
                new Event("Bob", "./prod3", 8000L),
                new Event("Alice", "./prod2", 4000L),
                new Event("Alice", "./prod2", 4000L),
                new Event("Alice", "./prod2", 4000L),
                new Event("Alice", "./prod2", 4000L),
                new Event("Alice", "./prod2", 4000L));

//        source.shuffle().print("shuffle").setParallelism(4);

        // 轮询分区
        source.rebalance().print("rebalance").setParallelism(4);

        executionEnvironment.execute();

    }
}
